import Taro, { Component } from '@tarojs/taro'
import {Image, Text, View, Input, Button  } from '@tarojs/components'
import { AtForm, AtInput, AtButton, AtIcon } from 'taro-ui'
import './login.scss'
import configs from "../../configuration";
import {request_good} from "../../untils/request";
import Header from '../../component/header/index'

export default class Index extends Component{

  config = {
    navigationBarTitleText: '登录'
  };

  constructor(props) {
    super(props)
    this.state = {
      name: '',
      password: ''
    }
  }

  handleChangeName (name) {
    /*console.log(name)*/
    this.setState({
      name: name.detail.value
    })
  }

  handleChangePassword (password) {

    this.setState({
      password: password.detail.value
    })
  }

  getyz (name) {

  }

  onLogin (name, password) {
    var obj = {
      'phone': name,
      'password': password,
      'app_id': configs.app_id,
      'source': 4
    }
    var post_login = {
      url : configs.apis['login'],
      method : 'post',
      data : obj
    }
    request_good(post_login).then(res => {

      if (res.code != 0 ){
        console.log(res)
      }else {
        var token = res.res.token;
        Taro.setStorageSync('access_token', token.access_token)
        Taro.setStorageSync('refresh_token', token.refresh_token)
        Taro.navigateBack()
      }
    }).catch(err => {
      console.log('waibu',err)
    });
    console.log(name,password)
  }

  render() {
    var hedaer = <Header name={this.config.navigationBarTitleText} />

    var env = Taro.getEnv().toUpperCase()
    console.log(env)

    return (
      <View>
        {env === 'WEB' ? hedaer : '' }
        <Input
          name='name'
          type='number'
          placeholder='请输入手机号'
          value={this.state.name}
          onInput={this.handleChangeName.bind(this)}
          className='user_name'
        />
        <View></View>
        <Input
          name='password'
          type='password'
          placeholder='请输入验证码'
          value={this.state.password}
          onInput={this.handleChangePassword.bind(this)}
          className='password'
        />
        <Button  onClick={this.getyz.bind(this, this.state.name)} >获取验证码</Button >
        <Button  type='primary' onClick={this.onLogin.bind(this, this.state.name, this.state.password)}>登录</Button >
      </View>
    )
  }

}
